package com.stedu.mall.goodsservice.controller;

import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.Goods;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;


    //添加
    @PostMapping
    public RespBean insert(@RequestBody Goods goods) throws SteduException {
        goods.setScore(5f);
        goodsService.insert(goods);
        return RespBean.ok("添加成功");
    }
@DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id) throws SteduException {
        goodsService.delete(id);
        return RespBean.ok("删除成功");
    }
    @PutMapping
    public RespBean update(@RequestBody Goods goods) throws SteduException {
        goodsService.update(goods);
        return RespBean.ok("修改成功");
    }
    @GetMapping
    public RespBean selectByPage(Goods condition,Integer pageNum,Integer pageSize) throws SteduException {
        pageNum = pageNum==null ? 1:pageNum;
        pageSize = pageSize==null ? 5:pageSize;
        PageInfo<Goods> pageInfo =goodsService.selectByCondition(condition, pageNum, pageSize);
        return RespBean.ok("查询成功",pageInfo);
    }
    @GetMapping("/search")
    public RespBean selectByPage1(Goods condition,Integer pageNum,Integer pageSize) throws SteduException {
        pageNum = pageNum==null ? 1:pageNum;
        pageSize = pageSize==null ? 5:pageSize;
        PageInfo<Goods> pageInfo =goodsService.selectByCondition1(condition, pageNum, pageSize);
        return RespBean.ok("查询成功",pageInfo);
    }

    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") Integer id) throws SteduException {
        Goods data=new Goods();
        data= goodsService.selectById(id);
       return RespBean.ok("查询成功",data);
    }
    //图表
    @GetMapping("/selectByGroupByCategory")
    public RespBean selectByGroupByCategory() {
        List<Map<String, Object>> goodsList = goodsService.selectByGroupByCategory();
        return RespBean.ok("", goodsList);
    }

}

